﻿/******************************************************************************
 *      Project:            Weather Station Program (WPF)
 *      Authors:            Michael Slezak, Samuel Harrington
 *      Due Date:           12/09/2011
 *      Class:              CSC 421 - GUI
 *      Instructor:         John Weiss, Ph.D.
 *      Environment:        Visual Studio 2010
 *      External Libraries: WPF Toolkit, included in ThirdParty
 *      Notes:              REQURES .NET 4.0!
 *      
 *-----------------------------------------------------------------------------
 *
 *      Description:
 *          This project provides a graphical user interface solution for
 *      describing large accumulations of weather data described in an XML
 *      file format. The user may direct the program to a particular directory
 *      which might contain the weather data. The program will automatically
 *      load in the whole directory and display results in the chart. The 
 *      program will also aggregate the XML data to display results in 
 *      labels just below the chart. The user will be able to query specific
 *      start and end dates, pan from each day/week/month/year, or hover on a
 *      data point to reveal statistics about that specific point using the 
 *      tooltip.
 * 
 ******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
//   INCLUDES
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;

namespace WeatherStationProgram
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IDisposable
    {
        private WeatherData _weatherData;        

        /**********************************************************************
         *      Function: MainWindow
         *      Authors:  Michael Slezak, Samuel Harrington    
         *      Returns:  void
         *      Purpose:  The constructor for the main window. Sets the data
         *                context for databinding and implements the window
         *                Closing event.
         * ********************************************************************/
        public MainWindow()
        {
            InitializeComponent();            

            _weatherData = new WeatherData();            
            DataContext = _weatherData; // used for the databinding                    
            Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
            //WeatherLineSeries.DependentValuePath = _weatherData.DataDisplayed.ToString();            
        }

        /***********************************************************************
         *      Function: Dispose
         *      Authors:  Michael Slezak, Samuel Harrington
         *      Returns:  void
         *      Purpose:  Part of the IDisposable interface. Always a good idea
         *                to detach from events when cleaning up.
         * *********************************************************************/
        public void Dispose()
        {
            Closing -= MainWindow_Closing;
        }

        /***********************************************************************
         *      Function: MainWindow_Closing
         *      Authors:  Michael Slezak, Samuel Harrington
         *      Parameters:
         *          object sender - the object which sent the event
         *          CancelEventArgs e - Event arguments describing the detail of the event
         *      Returns:  void
         *      Purpose:  This is a simple event hander which double-checks
         *                if the user really wants to exit the program.
         * *********************************************************************/
        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (System.Windows.MessageBox.Show("Are you sure you want to quit?", "Exit Application", MessageBoxButton.YesNo, MessageBoxImage.Question)
                == MessageBoxResult.No)
                e.Cancel = true;

        }

        /***********************************************************************
         *      Function: OpenMenuItem_Click
         *      Authors:  Michael Slezak, Samuel Harrington
         *      Returns:  void
         *      Parameters:
         *          object sender - the object which sent the event
         *          RoutedEventArgs e - Event arguments describing the detail of the event
         *      Purpose:  Event handler for handling the Open menu item. Simply,
         *                helps the user pick a folder to load their data from.
         * *********************************************************************/
        private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            
            DialogResult result = folder.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                _weatherData.SetDirectory(folder.SelectedPath);
            }
        }

        /***********************************************************************
         *      Function: ExitMenuItem_Click
         *      Authors:  Michael Slezak, Samuel Harrington
         *      Parameters:
         *          object sender - the object which sent the event
         *          RoutedEventArgs e - Event arguments describing the detail of the event
         *      Returns:  void
         *      Purpose:  Event handler for when the user selects the Exit menu item.
         * *********************************************************************/
        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /***********************************************************************
         *      Function: AboutMenuItem_Click
         *      Authors:  Michael Slezak, Samuel Harrington
         *      Parameters:
         *          object sender - the object which sent the event
         *          RoutedEventArgs e - Event arguments describing the detail of the event
         *      Returns:  void
         *      Purpose:  A message box about the application.
         * *********************************************************************/
        private void AboutMenuItem_Click(object sender, RoutedEventArgs e)
        {
            string msg = "Weather Station Program (WPF)\n\n";
            msg += "Authors: Michael Slezak, Samuel Harrington\n";
            msg += "Instructor: John Weiss, Ph.D.\n";
            msg += "Class: CSC 421 - GUI\n";

            // there was ambiguity between System.Windows.Forms and System.Windows.MessageBox. Wanted to use the latter
            System.Windows.MessageBox.Show(msg, "About Weather Station Program...", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        /***********************************************************************
        *      Function: PreviousButton_Click
        *      Authors:  Michael Slezak, Samuel Harrington
        *      Parameters:
        *          object sender - the object which sent the event
        *          RoutedEventArgs e - Event arguments describing the detail of the event
        *      Returns:  void
        *      Purpose:  Event handler which tells the WeatherData class to grab
         *               the previous set of data.
        * *********************************************************************/
        private void PreviousButton_Click(object sender, RoutedEventArgs e)
        {
            _weatherData.Prev();            
        }

        /***********************************************************************
        *      Function: NextButton_Click
        *      Authors:  Michael Slezak, Samuel Harrington
        *      Parameters:
        *          object sender - the object which sent the event
        *          RoutedEventArgs e - Event arguments describing the detail of the event
        *      Returns:  void
        *      Purpose:  Event handler which tells the WeatherData class to grab
         *               the next set of data.
        * *********************************************************************/
        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            _weatherData.Next();
        }

        /***********************************************************************
        *      Function: DataComboBox_SelectionChanged
        *      Authors:  Michael Slezak, Samuel Harrington
        *      Parameters:
        *          object sender - the object which sent the event
        *          SelectionChangedEventArgs e - Event arguments describing the detail of the event
        *      Returns:  void
        *      Purpose:  Event handler which changes the Dependent axis of the
         *               chart based on the selection of the data (temperature,
         *               humidity, barometric pressure, etc.). Could not find
         *               a way to implement databinding on this part.
        * *********************************************************************/
        private void DataComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            WeatherLineSeries.DependentValuePath = _weatherData.DataDisplayed.ToString();
        }               
    }
}
